/**
 * 
 */
package info.niwota.group.thread;

import info.niwota.group.R;
import info.niwota.group.Utils;
import info.niwota.group.WebViewActivity;
import info.niwota.group.thread.Topic.Threads;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

/**
 * @author qiangli
 *
 */
public class MainActivity extends TopicListActivity {
	
	private static final String HELP_URI = "file:///android_asset/help.html";

	private void doHelp() {
		//showDialog(DIALOG_HELP);
		Intent i = new Intent(this, WebViewActivity.class);
		i.setData( Uri.parse(HELP_URI));
		startActivity(i);
	}
	
	private void doQuit() {
		finish();
	}

//	private void doSettings() {
//		Intent i = new Intent(this, EtalkSettingActivity.class);
//		startActivity(i);
//	}
//
//	private void doSignature() {
//		Intent i = new Intent(this, SetupActivity.class);
//		int tab = SetupActivity.TAB_SIGNATURE;
//		i.putExtra(SetupActivity.EXTRA_TAB, tab);
//		startActivity(i);
//	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_options, menu);
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			doQuit();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
//		case R.id.main_add_opt_menu_id:
//			doAdd();
//			return true;
//		case R.id.main_setting_opt_menu_id:
//			doSettings();
//			return true;
//		case R.id.main_signature_opt_menu_id:
//			doSignature();
//			return true;
		case R.id.main_help_opt_menu_id:
			doHelp();
			return true;
		case R.id.main_quit_opt_menu_id:
			doQuit();
			return true;
		default:
			return false;
		}
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return true;
	}
	
	protected void updateTitle(String title, int cnt) {
		//
	}
	
	@Override
	protected void onListItemClick(ListView list, View view, int pos, long id) {
		try {
			Cursor c = mCursor;
			//
			long tid = TOPIC_ID_NONE;
			String name = "";
			String title = "";
			String body = "";
			String emails = "";
			
			updatePreference(tid);
			
			if (pos != -1) {
				c.moveToPosition(pos);
				tid = c.getLong(c.getColumnIndexOrThrow(Threads._ID));
				name = c.getString(c.getColumnIndexOrThrow(Threads.NAME));
				title = c.getString(c.getColumnIndexOrThrow(Threads.TITLE));
				body = c.getString(c.getColumnIndexOrThrow(Threads.BODY));
				emails = c.getString(c.getColumnIndexOrThrow(Threads.EMAILS));
				
				//
				sendMessage(name, emails, title, body, null);
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void sendMessage(String name, String emails, String subject, String body, Uri uri) {	
		Intent i = new Intent();
		i.setAction(Intent.ACTION_SEND);
		i.setType("message/rfc822");
		
		//
		// i.setType("audio/3gpp");
		// i.setType("text/plain"); //emulator
		// i.setType("message/rfc822"); //phone
		//
		
		// email
		String[] tos = Utils.toStringArray(emails);
		
		i.putExtra(Intent.EXTRA_EMAIL, tos);
		i.putExtra(Intent.EXTRA_SUBJECT, subject);
		i.putExtra(Intent.EXTRA_TEXT, body);
		
		//
		if (uri != null) {
			i.putExtra(Intent.EXTRA_STREAM, uri);
		}
		
		//
		// mms
		i.putExtra("address", emails);
		i.putExtra("exit_on_sent", true);
		i.putExtra("sms_body", body);
		
		//
		//i.putExtra(EtalkIntent.EXTRA_NAME, name);
		//i.putExtra(EtalkIntent.EXTRA_URI, path);
		//i.putExtra(EtalkIntent.NOOP, noop);
		
		//
		String ti = getString(R.string.menu_composewith); 
		startActivity(Intent.createChooser(i, ti));
	}
}
